﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Xml.Linq;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=391641 上有介绍

namespace AppEO
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            this.NavigationCacheMode = NavigationCacheMode.Required;
            LoadAd();
            GetInfo();
        }

        void LoadAd()
        {            
            //this.surfaceAdStandard.OnApplicationAuthEvent += surfaceAdStandard_OnApplicationAuthEvent;            
        }

        void surfaceAdStandard_OnApplicationAuthEvent(object sender, SurfaceAd.Common.SurfaceAdEventArgs e)
        {
            Debug.WriteLine("surfaceAdStandard_OnApplicationAuthEvent" + Environment.NewLine + e.ToString());
        }

        private void SurfaceAdStandard_OnAdRequest(object sender, SurfaceAd.Common.SurfaceAdEventArgs e)
        {
            Debug.WriteLine("SurfaceAdStandard_OnAdRequest" + Environment.NewLine + e.ToString());
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。
        /// 此参数通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            // TODO: 准备此处显示的页面。
            Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;

            // TODO: 如果您的应用程序包含多个页面，请确保
            // 通过注册以下事件来处理硬件“后退”按钮:
            // Windows.Phone.UI.Input.HardwareButtons.BackPressed 事件。
            // 如果使用由某些模板提供的 NavigationHelper，
            // 则系统会为您处理该事件。
        }

        void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
        {
            e.Handled = true;
            Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
            if (Frame.CanGoBack)
                Frame.GoBack();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(BlankPage1));
            //Frame.Navigate(typeof(WebViewBrushPage));
        }

        private async void GetInfo()
        {
            StorageFile file = await Package.Current.InstalledLocation.GetFileAsync("AppxManifest.xml");
            string manifestXml = await FileIO.ReadTextAsync(file);
            XDocument doc = XDocument.Parse(manifestXml);
            XNamespace packageNamespace = "http://schemas.microsoft.com/appx/2010/manifest";
            var displayName = (from name in doc.Descendants(packageNamespace + "DisplayName") select name.Value).First();
            var publisherDsplName = (from publisher in doc.Descendants(packageNamespace + "PublisherDisplayName") select publisher.Value).First();
            string output = "DisplayName: " + displayName + ", PublisherDisplayName: " + publisherDsplName;
            // this.txtXmlFile.Text = output;
        }

        private void surfaceAdStandard_OnAdClick(object sender, SurfaceAd.Common.SurfaceAdEventArgs e)
        {
            if (e != null)
            {
                switch (e.StatusCode)
                {
                    case SurfaceAd.Common.SurfaceAdStatus.AdContent_AdPositionClosed:
                        Debug.WriteLine(string.Concat("广告位被闭关：", e.ToString()));
                        break;
                    case SurfaceAd.Common.SurfaceAdStatus.AdContent_Click:
                        Debug.WriteLine(string.Concat("广告被点击：", e.ToString()));
                        break;
                    default:
                        Debug.WriteLine("surfaceAdStandard_OnAdClick" + Environment.NewLine + e.ToString());
                        break;
                }
            }
        }

        private void surfaceAdScreen_OnAdRequest(object sender, SurfaceAd.Common.SurfaceAdEventArgs e)
        {
            Debug.WriteLine("SurfaceAdScreen_OnAdRequest" + Environment.NewLine + e.ToString());            
        }

        private void surfaceAdScreen_OnAdClick(object sender, SurfaceAd.Common.SurfaceAdEventArgs e)
        {
            if (e != null)
            {
                switch (e.StatusCode)
                {
                    case SurfaceAd.Common.SurfaceAdStatus.AdContent_AdPositionClosed:
                        Debug.WriteLine(string.Concat("广告位被闭关：", e.ToString()));
                        break;
                    case SurfaceAd.Common.SurfaceAdStatus.AdContent_Click:
                        Debug.WriteLine(string.Concat("广告被点击：", e.ToString()));
                        break;
                    default:
                        Debug.WriteLine("SurfaceAdScreen_OnAdClick" + Environment.NewLine + e.ToString());
                        break;
                }
            }
        }



        #region 视频广告
        private void surfaceAdStandardVideo_OnAdRequest(object sender, SurfaceAd.Common.SurfaceAdEventArgs e)
        {
            Debug.WriteLine("surfaceAdStandardVideo_OnAdRequest" + Environment.NewLine + e.ToString()); 
        }

        private void surfaceAdStandardVideo_OnAdClick(object sender, SurfaceAd.Common.SurfaceAdEventArgs e)
        {
            if (e != null)
            {
                switch (e.StatusCode)
                {
                    case SurfaceAd.Common.SurfaceAdStatus.AdContent_AdPositionClosed:
                        Debug.WriteLine(string.Concat("广告位被闭关：", e.ToString()));
                        break;
                    case SurfaceAd.Common.SurfaceAdStatus.AdContent_Click:
                        Debug.WriteLine(string.Concat("广告被点击：", e.ToString()));
                        break;
                    default:
                        Debug.WriteLine("surfaceAdStandardVideo_OnAdClick" + Environment.NewLine + e.ToString());
                        break;
                }
            }
        }

        private void surfaceAdStandardVideo_OnApplicationAuthEvent(object sender, SurfaceAd.Common.SurfaceAdEventArgs e)
        {
            Debug.WriteLine("surfaceAdStandardVideo_OnApplicationAuthEvent" + Environment.NewLine + e.ToString()); 
        }
        #endregion 视频广告




    }
}
